home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte28 / helptest.c < prev    next >
C/C++ Source or Header  |  1995-12-28  |  9KB  |  275 lines

  1.  
  2. #include <windows.h>  
  3. #include "helptest.h"  
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "Help Test Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_LOADREALSIZE );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109.    switch( uMsg )
  110.    {
  111.       case WM_COMMAND :
  112.               switch( LOWORD( wParam ) )
  113.               {
  114.                  case IDM_HELP_TOPICS :
  115.                         WinHelp( hWnd, "HELPTEST.HLP", HELP_FINDER, 0 );
  116.                         break;
  117.                 
  118.                  case IDM_TEST :
  119.                         DialogBox( hInst, "HLPDialog", hWnd, (DLGPROC)HlpDialog );
  120.                         break;
  121.  
  122.                  case IDM_ABOUT :
  123.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  124.                         break;
  125.  
  126.                  case IDM_EXIT :
  127.                         DestroyWindow( hWnd );
  128.                         break;
  129.               }
  130.               break;
  131.       
  132.       case WM_DESTROY :
  133.               // Close Help file if it is displayed.
  134.               //....................................
  135.               WinHelp( hWnd, "HELPTEST.HLP",  HELP_QUIT, 0 );
  136.  
  137.               PostQuitMessage(0);
  138.               break;
  139.  
  140.       default :
  141.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  142.    }
  143.  
  144.    return( 0L );
  145. }
  146.  
  147.  
  148.  
  149. LRESULT CALLBACK HlpDialog( HWND hDlg,           
  150.                             UINT message,        
  151.                             WPARAM wParam,       
  152.                             LPARAM lParam)
  153. {
  154. static DWORD nIDs[] = { IDC_RADIO1, HELP_BASE+IDC_RADIO1,
  155.                         IDC_RADIO2, HELP_BASE+IDC_RADIO2,
  156.                         IDC_RADIO3, HELP_BASE+IDC_RADIO3,
  157.                         IDOK,       HELP_BASE+IDOK,
  158.                         IDCANCEL,   HELP_BASE+IDCANCEL,
  159.                         IDHELP,     HELP_BASE+IDHELP,
  160.                         0, 0 };
  161.  
  162.    switch (message) 
  163.    {
  164.        case WM_INITDIALOG : 
  165.                SetWindowLong( hDlg, GWL_EXSTYLE, 
  166.                               GetWindowLong( hDlg, GWL_EXSTYLE ) | WS_EX_CONTEXTHELP );
  167.  
  168.                return (TRUE);
  169.  
  170.        case WM_HELP :
  171.                {
  172.                   int i = 0;
  173.                   LPHELPINFO lpInfo = (LPHELPINFO)lParam;
  174.  
  175.                   // Make sure that the window is within our help IDs.
  176.                   // This elliminates the "No help..." message.
  177.                   //..................................................
  178.                   while( nIDs[i] )
  179.                   {
  180.                      if ( nIDs[i] == (DWORD)GetWindowLong( lpInfo->hItemHandle, GWL_ID ) )
  181.                      {
  182.                         WinHelp( lpInfo->hItemHandle, "HELPTEST.HLP", HELP_WM_HELP, (DWORD)(LPVOID)nIDs );
  183.                         break;
  184.                      }
  185.                      i+=2;
  186.                   }
  187.                }
  188.                break;
  189.  
  190.        case WM_CONTEXTMENU :
  191.                {
  192.                   int i = 0;
  193.  
  194.                   // Make sure that the window is within our help IDs.
  195.                   // This elliminates the "No help..." message.
  196.                   //..................................................
  197.                   while( nIDs[i] )
  198.                   {
  199.                      if ( nIDs[i] == (DWORD)GetWindowLong( (HWND)wParam, GWL_ID ) )
  200.                      {
  201.                         WinHelp( (HWND)wParam, "HELPTEST.HLP", HELP_CONTEXTMENU, (DWORD)(LPVOID)nIDs );
  202.                         break;
  203.                      }
  204.                      i+=2;
  205.                   }
  206.                }
  207.                break;
  208.  
  209.        case WM_TCARD :
  210.                switch( wParam )
  211.                {
  212.                   case IDOK : 
  213.                          EndDialog( hDlg, IDOK );
  214.                          break;
  215.  
  216.                   case IDCANCEL :
  217.                          EndDialog( hDlg, IDCANCEL );
  218.                          break;
  219.  
  220.                   case IDABORT :
  221.                          EnableWindow( hDlg, TRUE );
  222.                          break;
  223.  
  224.                   case HELP_TCARD_DATA :
  225.                          CheckRadioButton( hDlg, IDC_RADIO1, IDC_RADIO3, lParam );
  226.                          break;
  227.                }
  228.                break;
  229.  
  230.        case WM_COMMAND:                              
  231.                switch( LOWORD( wParam ) )
  232.                {
  233.                   case IDOK :
  234.                          EndDialog( hDlg, IDOK );
  235.                          break;
  236.  
  237.                   case IDCANCEL :
  238.                          EndDialog( hDlg, IDCANCEL );
  239.                          break;
  240.  
  241.                   case IDHELP :
  242.                          WinHelp( hDlg, "HELPTEST.HLP", HELP_TCARD | HELP_CONTEXT, IDH_TRAINING );
  243.                          EnableWindow( hDlg, FALSE );
  244.                          break;
  245.                }
  246.                break;
  247.    }
  248.  
  249.    return (FALSE); 
  250. }
  251.  
  252.  
  253. LRESULT CALLBACK About( HWND hDlg,           
  254.                         UINT message,        
  255.                         WPARAM wParam,       
  256.                         LPARAM lParam)
  257. {
  258.    switch (message) 
  259.    {
  260.        case WM_INITDIALOG: 
  261.                return (TRUE);
  262.  
  263.        case WM_COMMAND:                              
  264.                if (   LOWORD(wParam) == IDOK         
  265.                    || LOWORD(wParam) == IDCANCEL)    
  266.                {
  267.                        EndDialog(hDlg, TRUE);        
  268.                        return (TRUE);
  269.                }
  270.                break;
  271.    }
  272.  
  273.    return (FALSE); 
  274. }
  275.